package com.gqb.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.alibaba.fastjson.JSONObject;


public class restClient {
	public static JSONObject getRestdata(String urlString, String type) {
		try {

			@SuppressWarnings({ "resource", "deprecation" })
			DefaultHttpClient httpClient = new DefaultHttpClient();
			if (type.equals("post")) {

			} else {

			}
			HttpGet getRequest = new HttpGet(urlString);
			getRequest.addHeader("accept", "application/json");
			HttpPost postRequest = new HttpPost(urlString);
			postRequest.setHeader("accept", "application/json");
			HttpResponse response;
			if (type.equals("post")) {
				response = httpClient.execute(postRequest);
			}else{
				response = httpClient.execute(getRequest);
			}
			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatusLine().getStatusCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(response.getEntity().getContent()), "utf-8"));
			
			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
				JSONObject jsonObject = JSONObject.parseObject(output);
				
				return jsonObject;
			}
			httpClient.getConnectionManager().shutdown();
		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}
}
